<!DOCTYPE HTML>
<script src="../../../resources/testharness.js"></script>
<script src="../../../resources/testharnessreport.js"></script>
<script src="../../../resources/gesture-util.js"></script>
<script src="../../../virtual/percent-based-scrolling/resources/percent-based-util.js"></script>
<style>
#overscrollXDiv {
  width: 600px;
  height: 600px;
  overflow: scroll;
  overscroll-behavior-x: contain;
}
#overscrollYDiv {
  width: 500px;
  height: 500px;
  overflow: scroll;
  overscroll-behavior-y: none;
}
#targetDiv {
  width: 400px;
  height: 400px;
  overflow: scroll;
}
.content {
  width:800px;
  height:800px;
}
</style>

<body style="margin:0">
<div id="overscrollXDiv">
  <div class=content>
    <div id="overscrollYDiv">
      <div class=content>
        <div id="targetDiv">
          <div class="content">
          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>

<script>
if (window.internals)
  internals.runtimeFlags.overscrollCustomizationEnabled = true;

var target_div = document.getElementById('targetDiv');
var rect = target_div.getBoundingClientRect();
var x = rect.left + rect.width / 2;
var y = rect.top + rect.height / 2;

// SPEED_INSTANT doesn't work here because tests are timing out.
// This could actually be a bug in how the overscroll event is computed.
// Possible explanation on what happens is that "leftover" delta from a single
// event doesn't cause an overscroll event, only when the next event arrives
// and the full delta is overscroll do we dispatch the event.
// When the speed is INSTANT we'll put the entire scrolling delta into a single
// event so we don't generate an overscroll event in that case.
const pixelsPerSec = 2000;
const pixelsToScroll = 600;
const { x: pixelsToScrollX, y: pixelsToScrollY } = calculatePixelsToScroll(
  target_div, pixelsToScroll, pixelsToScroll
);

var overscrolled_x_delta = 0;
var overscrolled_y_delta = 0;
function onOverscrollX(event) {
  assert_false(event.cancelable);
  assert_false(event.bubbles);
  overscrolled_x_delta = event.deltaX;
}
function onOverscrollY(event) {
  assert_false(event.cancelable);
  assert_false(event.bubbles);
  overscrolled_y_delta = event.deltaY;
}
// Test that both "onoverscroll" and addEventListener("overscroll"... works.
document.getElementById('overscrollXDiv').onoverscroll = onOverscrollX;
document.getElementById('overscrollYDiv').
    addEventListener("overscroll", onOverscrollY);

async function test_overscroll_event_fire(source_device,
    precise_scrolling_delta) {
  overscrolled_x_delta = 0;
  overscrolled_y_delta = 0;
  await waitForCompositorCommit();

  // Scroll up on target div and wait for the element with overscroll-y to get
  // overscroll event.
  await smoothScroll(
    pixelsToScrollY, x, y, source_device, "up", pixelsPerSec,
    precise_scrolling_delta
  );
  await waitFor(() => { return overscrolled_y_delta < 0; },
      'Expected element did not receive overscroll event after scroll up on ' +
      'target.');
  assert_equals(target_div.scrollTop, 0);

  // Scroll left on target div and wait for the element with overscroll-x to
  // get overscroll event.
  await smoothScroll(
    pixelsToScrollX, x, y, source_device, "left", pixelsPerSec,
    precise_scrolling_delta
  );
  await waitFor(() => { return overscrolled_x_delta < 0; },
      'Expected element did not receive overscroll event after scroll left ' +
      'on target.');
  assert_equals(target_div.scrollLeft, 0);
}

promise_test (async (t) => {
  // Make sure that no overscroll event is sent to document or target_div.
  document.addEventListener("overscroll",
      t.unreached_func("Document got unexpected overscroll event."));
  target_div.addEventListener("overscroll",
      t.unreached_func("target_div got unexpected overscroll event."));
  return test_overscroll_event_fire(GestureSourceType.TOUCH_INPUT);
}, 'Tests that the last element in the cut scroll chain gets overscroll ' +
   'event when no element scrolls by touch.');

promise_test (async (t) => {
  // Make sure that no overscroll event is sent to document or target_div.
  document.addEventListener("overscroll",
      t.unreached_func("Document got unexpected overscroll event."));
  target_div.addEventListener("overscroll",
      t.unreached_func("target_div got unexpected overscroll event."));
  return test_overscroll_event_fire(GestureSourceType.MOUSE_INPUT,
      true /* precise_scrolling_delta */);
}, 'Tests that the last element in the cut scroll chain gets overscroll ' +
   'event when no element scrolls by wheel in non-animated scroll path.');

promise_test (async (t) => {
  // Make sure that no overscroll event is sent to document or target_div.
  document.addEventListener("overscroll",
      t.unreached_func("Document got unexpected overscroll event."));
  target_div.addEventListener("overscroll",
      t.unreached_func("target_div got unexpected overscroll event."));
  return test_overscroll_event_fire(GestureSourceType.MOUSE_INPUT,
      false /* precise_scrolling_delta */);
}, 'Tests that the last element in the cut scroll chain gets overscroll ' +
   'event when no element scrolls by wheel in smooth-scrolling.');
</script>
